package top.drinyeit.weilong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.model.ChildrenOrderDetailBean;

/**
 * Created by wu on 2018/1/13.
 */

public class ChildrenOrderDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private ChildrenOrderDetailBean childrenOrderDetailBean;
    private Context context;

    public ChildrenOrderDetailAdapter(ChildrenOrderDetailBean childrenOrderDetailBean, Context context){
        this.childrenOrderDetailBean = childrenOrderDetailBean;
        this.context = context;
    }

    public class OrderTitViewHolder extends RecyclerView.ViewHolder{
        public TextView store_name;
        public TextView order_state;
        public OrderTitViewHolder(View itemView) {
            super(itemView);
            store_name = itemView.findViewById(R.id.store_name);
            order_state = itemView.findViewById(R.id.order_state);
        }
    }

    public class OrderDetailViewHolder extends RecyclerView.ViewHolder{
        public TextView commodity_name;
        public TextView commodity_price;
        public TextView commodity_sum;
        public ImageView img;
        public OrderDetailViewHolder(View itemView) {
            super(itemView);
            commodity_name = itemView.findViewById(R.id.commodity_name);
            commodity_price = itemView.findViewById(R.id.commodity_price);
            commodity_sum = itemView.findViewById(R.id.commodity_sum);
            img = itemView.findViewById(R.id.img);
        }
    }

    public class OrderTotalViewHolder extends RecyclerView.ViewHolder{
        public TextView commodity_total_sum;
        public TextView order_price;
        public TextView express_price;
        public OrderTotalViewHolder(View itemView) {
            super(itemView);
            commodity_total_sum = itemView.findViewById(R.id.commodity_total_sum);
            order_price = itemView.findViewById(R.id.order_price);
            express_price = itemView.findViewById(R.id.express_price);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        context = parent.getContext();
        switch (viewType){
            case 0:
                return new OrderTitViewHolder(LayoutInflater.from(context).inflate(R.layout.item_order_title,parent,false));
            case 1:
                return new OrderDetailViewHolder(LayoutInflater.from(context).inflate(R.layout.item_order_merchandise,parent,false));
            case 2:
                return new OrderTotalViewHolder(LayoutInflater.from(context).inflate(R.layout.item_order_total,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemType  = getItemViewType(position);
        int realPosition = position - 1;
        switch (itemType){
            case 0:
                ((OrderTitViewHolder)holder).store_name.setText(childrenOrderDetailBean.getOrder_detail().getStore_name());
                ((OrderTitViewHolder)holder).order_state.setText("" + childrenOrderDetailBean.getOrder_detail().getOrder_state());
                break;
            case 1:
                ((OrderDetailViewHolder)holder).commodity_name.setText(childrenOrderDetailBean.getOrder_detail().
                                 getCommodity_list().get(realPosition).getCommodity_name());
                ((OrderDetailViewHolder)holder).commodity_price.setText(childrenOrderDetailBean.getOrder_detail().
                        getCommodity_list().get(realPosition).getCommodity_price() + "");
                ((OrderDetailViewHolder)holder).commodity_sum.setText("x" + childrenOrderDetailBean.getOrder_detail().
                        getCommodity_list().get(realPosition).getCommodity_sum());
                Glide.with(context).load(childrenOrderDetailBean.getOrder_detail().getCommodity_list().get(realPosition).getCommodity_picture())
                        .dontTransform()
                        .placeholder(R.drawable.ic_def)
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .into(((OrderDetailViewHolder) holder).img);
                System.out.println("pic:" + childrenOrderDetailBean.getOrder_detail().getCommodity_list().get(realPosition).getCommodity_picture());
                break;
            case 2:
                ((OrderTotalViewHolder)holder).commodity_total_sum.setText("¥" + childrenOrderDetailBean.getOrder_detail().getCommodity_total_sum());
                ((OrderTotalViewHolder)holder).order_price.setText(childrenOrderDetailBean.getOrder_detail().getOrder_price());
                ((OrderTotalViewHolder)holder).express_price.setText(childrenOrderDetailBean.getOrder_detail().getExpress_price() + ")");
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return 0;
        }else if (position == getItemCount() - 1){
            return 2;
        }else {
            return 1;
        }
    }

    @Override
    public int getItemCount() {
        return childrenOrderDetailBean.getOrder_detail().getCommodity_list().size() + 2;
    }
}
